package pixelwaks.gui.impl;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

import javax.swing.JComponent;

import common.gui.IViewable;
import common.util.FileHandler;

import pixelwaks.core.impl.Game;
import pixelwaks.core.impl.Player;
import pixelwaks.core.impl.Weapon;
import pixelwaks.ctrl.ControlFactory;

/**
 * Class for drawing a weapon selection view for a Player in a PlayerInfoPanel. 
 * Shows the weapons available for selecting and their remaining ammo, while greying out
 * weapons which have no ammo left. 
 * The Player's currently selected weapon will be highlighted.
 * Also indicates whether or not the Player is currently the active Player in 
 * two-player games.
 * The view is updated to display the currently correct information upon calling updateView().
 * @author Joakim
 *
 */
public class WeaponSelectView extends JComponent implements IViewable {
	
	private static final long serialVersionUID = 1L;
	private List<Weapon> weapons;
	private BufferedImage inf;
	private Player player;
	private int cellWidth;
	private int cellHeight;
	private int xPos;
	private int yPos;
	private int cellYPos;
	private int width;
	private int height;
	private Font arial10;
	private Font arialBlack11;
	
	/**
	 * Creates a weapon selection view and draws it.
	 * @param player the Player whose weapon information is to be displayed
	 */
	public WeaponSelectView(Player player) {
		
		this.player = player;
		weapons = player.getWeapons();
		
		//geometries
		xPos = 10;
		yPos = 10;
		cellYPos = yPos + 7;
		cellWidth = 68;
		width = weapons.size()*cellWidth + xPos*2 + 3;
		height = 43 + yPos*2;
		cellHeight = height - yPos*2 - 7;
		
		setPreferredSize(new Dimension(width, height));
		setVisible(true);
		
		ControlFactory.getObserver(ControlFactory.WEAPON_CTRL).addView(this);
		
		try {
			inf = FileHandler.getImage("", "infinity.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//fonts
		arial10 = new Font("Arial", Font.PLAIN, 10);
		arialBlack11 = new Font("Arial Black", Font.PLAIN, 11);
		new Font("Arial Black", Font.PLAIN, 12);
	}

	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(Color.BLACK);
		int cellXPos = xPos;
		int weaponNo = 1;
		
		for(Weapon weapon : weapons) {
			int numberOfShots = weapon.getNumberOfShots();
			Color selectedFillColor;
			Color unselectedFillColor;
			if(numberOfShots != 0) {
				selectedFillColor = Color.YELLOW;
				unselectedFillColor = Color.LIGHT_GRAY;
			}
			else {
				selectedFillColor = Color.DARK_GRAY;
				unselectedFillColor = Color.DARK_GRAY;
			}
			if(Game.getInstance().getCurrentPlayer() == player && weapon == player.getCurrentWeapon()) {
				drawSelectedCell(cellXPos, weaponNo, selectedFillColor, g2);
			}
			else {
				drawUnselectedCell(cellXPos, weaponNo, unselectedFillColor, g2);
			}
			
			drawTextImg(cellXPos, weapon, numberOfShots, g2);
			
			cellXPos += cellWidth;
			weaponNo++;
		}
	}

	@Override
	public void updateView(Object o) {
		repaint();		
	}
	
	private void drawSelectedCell(int cellXPos, int weaponNo, Color selectedFillColor, Graphics2D g2) {
		g2.setStroke(new BasicStroke(3));
		g2.drawRect(cellXPos+1, cellYPos-1, cellWidth-1, cellHeight-1);
		g2.setColor(selectedFillColor);
		g2.fillRect(cellXPos+8, yPos+2, 18, 10);
		g2.setColor(Color.BLACK);
		g2.drawRoundRect(cellXPos+6, yPos, 21, 15, 5, 5);
		g2.setColor(selectedFillColor);
		g2.fillRect(cellXPos+3, cellYPos+1, cellWidth-4, cellHeight-4);
		g2.setColor(Color.BLACK);
		g2.setFont(arialBlack11);
		g2.drawString("F" + weaponNo, cellXPos+10, yPos+12);
	}
	
	private void drawUnselectedCell(int cellXPos, int weaponNo, Color unselectedFillColor, Graphics2D g2) {
		g2.setStroke(new BasicStroke(2));
		g2.drawRect(cellXPos+1, cellYPos, cellWidth, cellHeight-2);
		g2.setColor(unselectedFillColor);
		g2.fillRect(cellXPos+8, yPos+2, 18, 10);
		g2.setColor(Color.BLACK);
		g2.drawRoundRect(cellXPos+7, yPos+1, 20, 15, 3, 3);
		g2.setColor(unselectedFillColor);
		g2.fillRect(cellXPos+2, cellYPos+1, cellWidth-2, cellHeight-4);
		g2.setColor(Color.BLACK);
		g2.setFont(arial10);
		g2.drawString("F" + weaponNo, cellXPos+11, yPos+11);
	}
	
	private void drawTextImg(int cellXPos, Weapon weapon, int numberOfShots, Graphics2D g2) {
		g2.setFont(arialBlack11);
		String shots;
		if(numberOfShots < 0) {
			shots = "x ";
			g2.drawString(shots, cellXPos+cellWidth - g2.getFontMetrics().stringWidth(shots) - inf.getWidth() - 5, cellYPos+15);
			g2.drawImage(inf, cellXPos+cellWidth - inf.getWidth() - 5, cellYPos+7, null);
		}
		else {
			shots = "x " + numberOfShots;
			g2.drawString(shots, cellXPos+cellWidth - g2.getFontMetrics().stringWidth(shots) - 5, cellYPos+15);
		}
		g2.setFont(arial10);
		String strength = "Str: " + weapon.getStrength();
		g2.drawString(strength, cellXPos+cellWidth - g2.getFontMetrics().stringWidth(strength) - 5, cellYPos + cellHeight - 8);
		BufferedImage img = weapon.getImage();
		g2.drawImage(img, cellXPos + 17 - img.getWidth()/2, cellYPos + cellHeight/2 - img.getHeight()/2, null);
	}

}
